
import prompt from '@system.prompt';
import router from '@system.router';

const ActionParam = {
    ABILITY_TYPE_EXTERNAL : 0,
    ABILITY_TYPE_INTERNAL : 1,
    // syncOption(Optional, default sync): 0-Sync; 1-Async
    ACTION_SYNC : 0,
    ACTION_ASYNC : 1,
}

function newActionBuilder() {
    var actionBuilder = {
        action : {},
        setBundleName : function(bundleName) {
            this.action.bundleName = bundleName;
            return this;
        },
        setAbilityName : function(abilityName) {
            this.action.abilityName = abilityName;
            return this;
        },
        setMessageCode : function(messageCode) {
            this.action.messageCode = messageCode;
            return this;
        },
        setAbilityType : function(abilityType) {
            this.action.abilityType = abilityType;
            return this;
        },
        setSyncOption : function(syncOption) {
            this.action.syncOption = syncOption;
            return this;
        },
        setData : function(data) {
            this.action.data = data;
            return this;
        },
        getAction() {
            return this.action;
        }
    }
    return actionBuilder;
}

function showToast(message) {
    var messageOb = {
        "message" : message,
        "duration" : 1500
    }
    prompt.showToast(messageOb);
}

const Action = {
    FLOWTO : 1001,
    FULLSCREEN : 1002,
    EXITFULLSCREEN : 1003,
    SAVE_CTX : 1004,
    LOAD_CTX : 1005
}

export default {
    data: {
        title: "",
        video_url: "",
        input_value: "",
        video_controls : true,
        config_panel_visibility : "hidden",
        current : 0,
        timestamp : 0,
        start_time : 0,
        input_comment : "",
        video_list: null,
        comments: null,
        app : null
    },
    onInit() {
        // 等待数据加载完成
        if (this.$app.$def.loading_promise != null) {
            this.$app.$def.loading_promise.then(this.doInitOnce).catch(this.loadingErr);
        } else {
            this.doInit();
        }
        console.log(this.current);
    },
    loadingErr(e) {
        console.log(e);
        showToast("Fail to load data");
    },
    doInitOnce() {
        if (this.video_list == null) {
            this.doInit();
        }
    },
    doInit() {
        try {
            this.video_list = this.$app.$def.global.video_list;
            this.comments = this.$app.$def.global.comments;
            this.current = this.$app.$def.global.video_index;
            this.video_url = this.video_list[this.current].url;
            this.start_time = this.$app.$def.global.current_time;
            this.input_comment = this.$app.$def.global.input_comment;
        } catch {
            console.log("do_init failed");
            this.video_list = [];
            this.comments = [];
            this.current = 0;
            this.video_url = ""
            this.start_time = 0
            this.input_comment = "";
            showToast("Fail to load data");
        }
    },
    onAddVideoClick() {
        if (this.input_value.length <= 0) {
            showToast(this.video_url);
            return;
        }
        this.video_list.push({
            "title" : this.video_list.length,
            "url" : this.input_value
        })
    },
    onVideoReady() {
        this.$element("video_player").setCurrentTime(
            {
                currenttime : this.start_time
            }
        );
        // 迁移的start_time只需要生效一次
        this.start_time = 0;
    },
    onClickVideo() {
        if (this.video_controls == true) {
            this.video_controls = false;
        } else {
            this.video_controls = true;
        }
        showToast(this.video_controls);
    },
    onVideoPlaying(e) {
        // 参数是一个事件对象，其中的currenttime属性才是播放进度
        this.$app.$def.global.current_time = e.currenttime;

    },
    onClickShowConfig() {
        // this.config_panel_visibility = "visible";
        router.push({
            uri: 'pages/setting/index',
            params : {}
        });
    },
    onClickFullScreen() {
        this.$element("video_player").requestFullscreen("default");
    },
    onFullScreenEvent(e) {
        var full_screen_status = e.fullscreen;
        // 查看fullscreen数据类型(使用typeof更好分辨数据类型)
        // showToast(full_screen_status)
        // 切换状态栏/导航栏
        if (full_screen_status) {
            this.enableFullScreen(true);
        } else {
            this.enableFullScreen(false);
        }
    },
    onSwipeVideo(e) {
        if (e.direction == "up") {
            this.current = (this.current + 1) % this.video_list.length;
            showToast("next");
        } else if (e.direction == "down") {
            this.current = (this.current + this.video_list.length - 1) % this.video_list.length;
            showToast("last");
        } else {
            return;
        }
        this.$app.$def.global.video_index = this.current;
        this.video_url = this.video_list[this.current].url;
    },
    onFlyAComment() {
        var find = false;
        for(var i=0; i<this.comments.length; i++) {
            if (this.comments[i] == '') {
                this.comments[i] = this.input_comment;
                find = true;
                this.comments.splice(0,0);
                break;
            }
        }
        if (find == false) {
            this.comments.push(this.input_comment);
        }
        showToast("send a comment");
    },
    onCommentDisappear(e) {
        var index = parseInt(e);
        this.comments[index] = '';
        this.comments.splice(0,0);
    },
    onInputCommentChange(e) {
        this.input_comment = e.value;
        this.$app.$def.global.input_comment = e.value;
    },
    async migrate() {
        console.info("click migrate ");
        FeatureAbility.continueAbility();
    },
    onStartContinuation(){
        // 判断当前的状态是不是适合迁移
        console.info("onStartContinuation");
        showToast("开始远程迁移...")
        return true;
    },
    onCompleteContinuation(code) {
        // 迁移操作完成，code返回结果
        console.info("nCompleteContinuation: code = " + code);
    },
    onSaveData(saveData) {
        // 将数据保存到savedData中进行迁移。
        Object.assign(saveData, this.$app.$def.global)
        console.info("onSaveData: data = " + JSON.stringify(saveData));
    },
    onRestoreData(restoreData) {
        // 收到迁移数据，恢复。
        this.$app.$def.global = restoreData;
        this.doInit();
        console.info("onRestoreData: data = " + JSON.stringify(restoreData));
        showToast("continue, start time" + this.start_time);
    },
    async enableFullScreen(enabled) {
        console.info("call java");
        var action_num;

        if (enabled) {
            action_num = Action.FULLSCREEN;
        } else {
            action_num = Action.EXITFULLSCREEN;
        }
        var actionData = {}
        var actionBuilder = newActionBuilder();
        var action = actionBuilder.setBundleName("top.january.codelabf2")
            .setAbilityName("top.january.codelabf2.InMainAbility")
            .setAbilityType(ActionParam.ABILITY_TYPE_INTERNAL)
            .setSyncOption(ActionParam.ACTION_SYNC)
            .setMessageCode(action_num)
            .setData(actionData)
            .getAction();
        return FeatureAbility.callAbility(action);
    },

}
